package gui;

import ge.escola.Disciplina;
import ge.escola.Turma;
import ge.util.Horario;
import ge.util.Validadores;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class HorarioTurmaPanel extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JTextField caixaCodigoDisciplina;
	private JButton buttonVoltar;
	private JTextField caixaHora;
	private JLabel jLabel7;
	private JLabel checagemHora;
	private JButton buttonRemover;
	private JButton buttonAdicionar;
	private JComboBox comboDias;
	private JTextArea textArea;
	Color verde = new Color(0, 150, 0);

	private Turma turma;

	private String dia, horaI, codigoDisciplina;
	boolean flag = true;
	Disciplina disciplina;
	Horario horario;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */

	public HorarioTurmaPanel() {
		super();
		initGUI();

		turma = SelecionaTurma.turma;
		atualizaHorarios();
	}

	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(800, 600);
			this.setLayout(null);
			this.setBackground(new java.awt.Color(204, 204, 255));
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Horarios da turma");
				jLabel1.setBounds(26, 12, 743, 55);
				jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36));
				jLabel1.setForeground(new java.awt.Color(0, 102, 102));
				jLabel1.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				textArea = new JTextArea();
				this.add(textArea);
				textArea.setBounds(26, 143, 346, 406);
				textArea.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
						null, null, null, null));
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Monte um horario");
				jLabel2.setBounds(469, 149, 231, 29);
				jLabel2.setFont(new java.awt.Font("Arial", 0, 16));
				jLabel2.setForeground(new java.awt.Color(0, 102, 102));
				jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel2.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				ComboBoxModel diasModel = new DefaultComboBoxModel(
						new String[] { "Segunda", "Terca", "Quarta", "Quinta",
								"Sexta", "Sabado", "Domingo" });
				comboDias = new JComboBox();
				this.add(comboDias);
				comboDias.setModel(diasModel);
				comboDias.setBounds(555, 210, 172, 23);
				comboDias.setFont(new java.awt.Font("Arial", 0, 12));
				comboDias.setBackground(new java.awt.Color(255, 255, 255));
				comboDias.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						comboDiasActionPerformed(evt);
					}
				});
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Dia:");
				jLabel3.setBounds(505, 212, 32, 16);
				jLabel3.setFont(new java.awt.Font("Arial", 0, 16));
				jLabel3.setForeground(new java.awt.Color(0, 102, 102));
			}
			{
				jLabel4 = new JLabel();
				this.add(jLabel4);
				jLabel4.setText("Hora inicial:");
				jLabel4.setBounds(451, 265, 86, 16);
				jLabel4.setFont(new java.awt.Font("Arial", 0, 16));
				jLabel4.setForeground(new java.awt.Color(0, 102, 102));
			}
			{
				buttonAdicionar = new JButton();
				this.add(buttonAdicionar);
				buttonAdicionar.setText("Adicionar horario");
				buttonAdicionar.setBounds(423, 368, 133, 23);
				buttonAdicionar.setFont(new java.awt.Font("Arial", 0, 14));
				buttonAdicionar
						.setBackground(new java.awt.Color(255, 255, 255));
				buttonAdicionar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							buttonAdicionarMouseClicked(evt);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
			{
				buttonRemover = new JButton();
				this.add(buttonRemover);
				buttonRemover.setText("Remover horario");
				buttonRemover.setFont(new java.awt.Font("Arial", 0, 14));
				buttonRemover.setBounds(579, 368, 133, 23);
				buttonRemover.setBackground(new java.awt.Color(255, 255, 255));
				buttonRemover.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							buttonRemoverMouseClicked(evt);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
			{
				checagemHora = new JLabel();
				this.add(checagemHora);
				checagemHora.setBounds(634, 265, 62, 15);
			}
			{
				buttonVoltar = new JButton();
				this.add(buttonVoltar);
				buttonVoltar.setText("VOLTAR");
				buttonVoltar.setBounds(527, 532, 92, 34);
				buttonVoltar.setBackground(new java.awt.Color(255, 255, 255));
				buttonVoltar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonVoltarMouseClicked(evt);
					}
				});
			}
			{
				jLabel7 = new JLabel();
				this.add(jLabel7);
				jLabel7.setText("Codigo da disciplina:");
				jLabel7.setFont(new java.awt.Font("Arial", 0, 16));
				jLabel7.setBounds(398, 315, 150, 21);
				jLabel7.setForeground(new java.awt.Color(0, 102, 102));
			}
			{
				caixaCodigoDisciplina = new JTextField();
				this.add(caixaCodigoDisciplina);
				caixaCodigoDisciplina.setBounds(555, 312, 172, 27);
				caixaCodigoDisciplina
						.setFont(new java.awt.Font("Arial", 0, 18));
				caixaCodigoDisciplina.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						caixaCodigoDisciplinaKeyReleased(evt);
					}
				});
			}
			{
				caixaHora = new JTextField();
				this.add(caixaHora);
				caixaHora.setBounds(555, 261, 72, 28);
				caixaHora.setFont(new java.awt.Font("Arial", 0, 20));
				caixaHora.setHorizontalAlignment(SwingConstants.CENTER);
				caixaHora.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						caixaHoraKeyReleased(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void comboDiasActionPerformed(ActionEvent evt) {
		dia = (String) comboDias.getSelectedItem();
	}

	private void atualizaHorarios() {
		String saida = "";
		for(Horario hora:turma.getListaHorarios()){
			saida += hora.toString() + "\n";
		}
		textArea.setEditable(true);
		textArea.setText(saida);
		textArea.setEditable(false);
	}

	private void caixaHoraKeyReleased(KeyEvent evt) {
		String hora = caixaHora.getText();
		if (Validadores.validaHora(hora)) {
			checagemHora.setText("OK");
			checagemHora.setForeground(verde);
			flag = true;
		} else {
			checagemHora.setForeground(Color.red);
			flag = false;
		}
	}

	private void buttonAdicionarMouseClicked(MouseEvent evt) throws Exception {
		if (turma.getSerie().getDisciplinasSerie().contains(disciplina)) {
			horario = new Horario(dia, horaI,GerenciadorDeJanelas.escola.getDuracaoAula(), disciplina);
			if (turma.getSerie().getDisciplinasSerie().contains(horario)) {
				JOptionPane.showMessageDialog(null,"Essa turma ja possui esse horario.", "ERRO",JOptionPane.ERROR_MESSAGE);
			} else {
				GerenciadorDeJanelas.todasTurmas.remove(turma);
				turma.addHorario(horario);
				GerenciadorDeJanelas.todasTurmas.add(turma);
				
				String xml = GerenciadorDeJanelas.ARQUIVADOR.toXML(GerenciadorDeJanelas.todasTurmas);
    			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeTurmas,xml);
    			
    			atualizaHorarios();
			}
		} else {
			JOptionPane.showMessageDialog(null,"A serie dessa turma nao possui essa disciplina.", "ERRO",JOptionPane.ERROR_MESSAGE);
		}

	}

	private void buttonRemoverMouseClicked(MouseEvent evt) throws Exception {
		if (turma.getSerie().getDisciplinasSerie().contains(disciplina)) {
			horario = new Horario(dia, horaI,GerenciadorDeJanelas.escola.getDuracaoAula(), disciplina);
			if (turma.getSerie().getDisciplinasSerie().contains(horario)) {
				GerenciadorDeJanelas.todasTurmas.remove(turma);
				turma.removeHorario(horario);
				GerenciadorDeJanelas.todasTurmas.add(turma);
				
				String xml = GerenciadorDeJanelas.ARQUIVADOR.toXML(GerenciadorDeJanelas.todasTurmas);
    			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeTurmas,xml);
				
    			atualizaHorarios();
			} else {
				JOptionPane.showMessageDialog(null,"Essa turma nao possui esse horario.", "ERRO",JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(null,"A serie dessa turma nao possui essa disciplina.", "ERRO",JOptionPane.ERROR_MESSAGE);
		}
	}

	private void caixaCodigoDisciplinaKeyReleased(KeyEvent evt) {
		codigoDisciplina = caixaCodigoDisciplina.getText();
	}

	private void capturaDisciplina() {
		for (Disciplina disc : GerenciadorDeJanelas.todasDisciplinas) {
			if (disc.getCodigo().equals(codigoDisciplina)) {
				disciplina = disc;
			}
		}
	}
	
	private void buttonVoltarMouseClicked(MouseEvent evt) {
		GerenciadorDeJanelas.alteraTurma = new AlteraTurma();
        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.alteraTurma);
        GerenciadorDeJanelas.window.setVisible(true);
	}
}